/*
 * PRESC1.c
 *
 *  Created on: 08/02/2013
 *      Author: RIchard
 */
#include "PRESC1.h"
#include <string.h>
#include "../../../sensor.h"
#include "../../../Driver/ADC/ADCC1.h"

static uint16 PRES__au16ADCBuffer[PRES_nSamples];
uint16 PRES__u16Value;

void PRES__vADCCallback( void );

/* All ADC samples have been take - Interrupt Context */
void PRES__vADCCallback( void )
{
   ADC_vStopSampling();
   /* set the app request to wake up and run the sensor core */
   APP_unRequests.F.biSensorReadRequest = 1;
}

void PRES_vInit( void )
{
   PRES__u16Value = 0;
}

void PRES_vSetPower( DIO_tenLogicLevel enState )
{
	DIO_vSetOutput( DIO_nenLED_Bar_1, enState );
}

bool PRES_boStartMeasure( void )
{
   ADC_tstRequest stADCRequest;

   stADCRequest.enChannel = ADC_nenPressure;
   stADCRequest.u8Samples = PRES_nSamples;
   stADCRequest.pu16DestBuffer = PRES__au16ADCBuffer;
   stADCRequest.pfOKCallback = &PRES__vADCCallback;

   ADC_vReadRequest( &stADCRequest );

	return True;
}

/*
 * Pressure Module Filter Function -
 * A simple average over all the samples
 */
void PRES_vFilter( void )
{
   uint8  u8Index;
   uint32 u32Temp;

   u32Temp = 0;

   for( u8Index = 0; u8Index < PRES_nSamples; u8Index++ )
   {
      u32Temp += PRES__au16ADCBuffer[u8Index];
   }

   PRES__u16Value = (uint16)( u32Temp >> 7 );
}
